﻿using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace suirui.ZhuMu.Common.Helper
{
    internal sealed class OSSClientSingleton
    {
        private static OssClient OSSClientInstance;
        private static readonly object locker = new object();
        public static OssClient GetInstance()
        {
            if (OSSClientInstance == null)
            {
                lock (locker)
                {
                    if (OSSClientInstance == null)//第一次为空判断解锁后第二次还要再判断否则会重新初始化
                    {
                        OSSClientInstance = new OssClient(ConfigurationHelper.GetConfigValueByKey("OSS_Endpoint"), ConfigurationHelper.GetConfigValueByKey("OSS_AccessKeyId"), ConfigurationHelper.GetConfigValueByKey("OSS_AccessKeySecret"));
                    }
                }
            }
            return OSSClientInstance;
        }
    }
    public class OSSClientHelper
    {
        private string AccessKeyId { get; set; }
        private string AccessKeySecret { get; set; }
        private string Endpoint { get; set; }
        private string BucketName { get; set; }
        private OSSClientHelper()
        {
            AccessKeyId = ConfigurationHelper.GetConfigValueByKey("OSS_AccessKeyId");
            AccessKeySecret = ConfigurationHelper.GetConfigValueByKey("OSS_AccessKeySecret");
            Endpoint = ConfigurationHelper.GetConfigValueByKey("OSS_Endpoint");
            BucketName = ConfigurationHelper.GetConfigValueByKey("OSS_BucketName");
        }
        public static OSSClientHelper DefaultInstance = new OSSClientHelper();
        public static OssClient GetClient()
        {
            return new OssClient(DefaultInstance.Endpoint, DefaultInstance.AccessKeyId, DefaultInstance.AccessKeySecret);
        }

        /// <summary>
        /// 上传一个图片
        /// </summary>
        /// <param name="base64Code">图片经过base64加密后的结果</param>
        /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
        public static bool PushImg(string base64Code, string fileName)
        {
            try
            {
                var client = GetClient();
                MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code));
                return client.PutObject(DefaultInstance.BucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 上传一个图片
        /// </summary>
        /// <param name="filebyte">图片字节 </param>
        /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
        public static bool PushImg(byte[] filebyte, string fileName)
        {
            try
            {
                var client = GetClient();
                MemoryStream stream = new MemoryStream(filebyte, 0, filebyte.Length);
                return client.PutObject(DefaultInstance.BucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取鉴权后的URL,URL有效日期默认3年
        /// </summary>
        /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
        /// <returns></returns>
        public static string GetImg(string fileName)
        {
            var client = GetClient();
            var key = fileName;
            var req = new GeneratePresignedUriRequest(DefaultInstance.BucketName, key, SignHttpMethod.Get)
            {
                //Expiration = DateTime.Now.AddHours(1)
                Expiration = DateTime.Now.AddYears(3)
            };
            return client.GeneratePresignedUri(req).ToString();
        }

        /// <summary>
        /// 获取鉴权后的URL
        /// </summary>
        /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param>
        /// <param name="expiration">URL有效日期,例如:DateTime.Now.AddHours(1) </param>
        /// <returns></returns>
        public static string GetImg(string fileName, DateTime expiration)
        {
            try
            {
                var client = GetClient();
                var key = fileName;
                var req = new GeneratePresignedUriRequest(DefaultInstance.BucketName, key, SignHttpMethod.Get)
                {
                    Expiration = expiration
                };
                return client.GeneratePresignedUri(req).ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static bool DetImg(string fileName)
        {
           var client = GetClient();
            try
            {
                client.DeleteObject(DefaultInstance.BucketName, fileName);
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
